package com.trantronghien85.websocketdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.content.Intent;
import com.google.common.net.InetAddresses;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Button;
import android.view.MenuItem;
import android.widget.EditText;
import androidx.core.app.NavUtils;
import android.view.View.OnClickListener;

public class SettingActivity extends AppCompatActivity {
	
    public static final int PICKFILE_RESULT_CODE = 1;    
	public static final String LOG_TAG = "Setting";
    TextView messageText;
    Button testButton;
    Button okButton;    	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
         
        testButton = (Button)findViewById(R.id.test);
        okButton = (Button)findViewById(R.id.ok);		
        messageText  = (TextView)findViewById(R.id.messageText);
		etIP = (EditText) findViewById(R.id.et_ip);	
		etPort = (EditText) findViewById(R.id.et_port);
        Config cfg=Config.get();
        cfg.load(this);
        if(cfg.host!=null){etIP.setText(cfg.host);}
        if(cfg.port>0){etPort.setText(Integer.toString(cfg.port));}

		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		//getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
		okButton.setOnClickListener(new Clicker1());
  	    testButton.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
            	
       
                
                new Thread(new Runnable() {
                        public void run() {
                             runOnUiThread(new Runnable() {
                                    public void run() {
                                    	messageText.setText("uploading started.....");
                                    }
                                });                      
                          
						     
                             //uploadFile( );
                                                     
                        }
                      }).start();        
                }
            });	

	}
    private class Clicker1 implements OnClickListener {
		public void onClick(View v) {
            String host = etIP.getText().toString();
            int port=Integer.valueOf(etPort.getText().toString());
            if(isValidIp(host)&&isValidPort(port)) {
                Config cfg=Config.get();
                cfg.host=host;
                cfg.port=port;
                cfg.save(SettingActivity.this);

                Intent myLocalIntent = getIntent();
                //Bundle myBundle= new Bundle();
                //myBundle.putString("ip", etIP.getText().toString());
                //myBundle.putString("port", etPort.getText().toString() );
                //myLocalIntent.putExtras(myBundle);

                setResult(Activity.RESULT_OK, myLocalIntent);
                finish();
            }
		} 	
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            //NavUtils.navigateUpFromSameTask(this);
			onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }	
	
	static public boolean isValidIp(String ip){
	     return InetAddresses.isInetAddress(ip);
	}
    static public boolean isValidPort(int port){return port<=65535 && port>0;}
	EditText etIP;
	EditText etPort;	
}